package com.cgc999.host.activity;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

import androidx.annotation.NonNull;

import com.cgc999.host.R;
import com.cgc999.host.adapter.FragmentsReplaceAdapter;
import com.cgc999.host.fragment.FoundFragment;
import com.cgc999.host.fragment.IndexFragment;
import com.cgc999.host.fragment.MineFragment;
import com.cgc999.host.fragment.PromoteListFragment;
import com.cgc999.host.service.MessageService;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;


public class MainActivity extends RxAppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemReselectedListener {

    private FragmentsReplaceAdapter mFragmentsReplaceAdapter;
    private static BottomNavigationViewEx mNavigation;

    static Badge[] badges=new Badge[4];

    private final String TAG="MainActivity";

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    long num=msg.getData().getLong("num");
                    if (num>0&&badges[1]==null) {
                        badges[1]=addBadgeAt(1,  (int)num);
                        foundFragment.reFreshData();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNavigation = findViewById(R.id.activity_main_navigation);
        initFragment();
        initNavigation();
        initData();

//        badges[1]=addBadgeAt(1,  (int)2);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Window window= this.getWindow();
        Log.i(TAG,window.toString());
//        startActivity(new Intent(this,AlbumActivity.class));
    }

    private  void initData(){
        Intent intent=new Intent(MainActivity.this,MessageService.class);
        intent.putExtra("messenger", new Messenger(handler));
        startService(intent);
    }

    FoundFragment foundFragment;

    private void initFragment() {
        foundFragment=new FoundFragment();
        mFragmentsReplaceAdapter = new FragmentsReplaceAdapter(getSupportFragmentManager(), R.id.content_frame, this);
        mFragmentsReplaceAdapter.addFragment(new IndexFragment());
        mFragmentsReplaceAdapter.addFragment(foundFragment);
        mFragmentsReplaceAdapter.addFragment(new PromoteListFragment());
        mFragmentsReplaceAdapter.addFragment(new MineFragment());
    }

    int currentRoutePage = 0;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        currentRoutePage = intent.getIntExtra("currentRoutePage", 0);
        if (mFragmentsReplaceAdapter != null) {
            mNavigation.setCurrentItem(currentRoutePage);
        }
    }

    /**
     * 初始化底部导航栏
     */
    private void initNavigation() {
        mNavigation.setOnNavigationItemSelectedListener(this);
        mNavigation.setOnNavigationItemReselectedListener(this);
        mNavigation.setTextVisibility(true);
        // 开启或关闭点击动画(文字放大效果和图片移动效果)。 默认为 true.
        mNavigation.enableAnimation(false);
        // 开始或关闭导航条位移模式。如果为 true，选中项和其他项的宽度不一样。当菜单数大于3时，默认为 true。
        mNavigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
        mNavigation.setItemHorizontalTranslationEnabled(false);
        mNavigation.setItemIconTintList(null);
        mNavigation.setTextSize(9);
        if (currentRoutePage == 0) {
            mFragmentsReplaceAdapter.setCurrentItem(0);
        } else {
            mNavigation.setCurrentItem(currentRoutePage);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
//        Log.i(TAG,menuItem.getItemId()+"");
        switch (menuItem.getItemId()) {
            case R.id.tab_one:
                mFragmentsReplaceAdapter.setCurrentItem(0);
//                AppConstant.video_ref = "";//这个要具体页面
//                MobclickAgent.onEvent(this, "homeActivity", "index_change_click");
//                addLogReq("首页");
                break;
            case R.id.tab_two:
                if (badges[1]!=null){
                    badges[1].hide(false);
                    badges[1]=null;
                }
                mFragmentsReplaceAdapter.setCurrentItem(1);
//                AppConstant.video_ref = "";//这个要具体页面
//                MobclickAgent.onEvent(this, "homeActivity", "jianwen_change_click");
//                addLogReq("鉴文");
                break;
            case R.id.tab_three:
                mFragmentsReplaceAdapter.setCurrentItem(2);
                break;
            case R.id.tab_four:
                mFragmentsReplaceAdapter.setCurrentItem(3);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
        Log.i(TAG,"onNavigationItemReselected:"+menuItem.getItemId()+"");
//        if (menuItem.getItemId() == R.id.tab_one){
//            //刷新
//            EventBus.getDefault().post(new IndexBottomRefreshEvent("shouye",""));
//        }else if (menuItem.getItemId() == R.id.tab_two){
//            //刷新
//            EventBus.getDefault().post(new IndexBottomRefreshEvent("jianwen",""));
//        }
    }

    private Badge addBadgeAt(int position, int number) {
        // add badge
        return new QBadgeView(this)
                .setBadgeNumber(number)
                .setGravityOffset(12, 2, true)
                .bindTarget(mNavigation.getBottomNavigationItemView(position))
                .setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                    @Override
                    public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                        Log.i("MainActivity","onDragStateChanged");
//                        badge.hide(true);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("MainActivity","ondestory");
    }
}
